﻿using System;
using System.Collections.Generic;
using System.Text;
using PostSharp.Aspects.Dependencies;

namespace Arcana.Contract.Property.Is.Not
{
    /// <summary>
    /// Represents a contract that adheres to <b>property is not an instance of</b> semantic.
    /// </summary>
    [AspectRoleDependency(AspectDependencyAction.Commute, Roles.Conditional)]
    [ProvideAspectRole(Roles.Contract)]
    [ProvideAspectRole(Roles.Property)]
    [ProvideAspectRole(Roles.Conditional)]
    [Serializable]
    public sealed class InstanceOfAttribute : ComparingContractBase
    {
        private Type _type;

        /// <summary>
        /// Initializes a new object of <b>Parameter.Is.Not.InstanceOfAttribute</b>.
        /// </summary>
        /// <param name="type">The type which our property should be a type of.</param>
        /// <exception cref="ArgumentNullException">Occurs when <i>type</i> is <b>null</b>.</exception>
        public InstanceOfAttribute(Type type)
        {
            if (type == null) throw new ArgumentNullException();
            _type = type;
        }

        /// <summary>
        /// Performs validation on the property.
        /// </summary>
        /// <param name="property">The property that should be validated.</param>
        /// <returns><b>true</b> if <i>property</i> is validated, otherwise <b>false</b>.</returns>
        protected override bool ValidateProperty(object property)
        {
            if (property == null) return true;
            return !property.GetType().Equals(_type);
        }
    }
}
